#ifndef SONYPERFORMANCEMONITOR_H
#define SONYPERFORMANCEMONITOR_H

#include <QObject>
#include <QTimer>
#include <QElapsedTimer>
#include <QHash>
#include <QQueue>
#include <QMutex>
#include "responsiveconfig.h"

/**
 * 🎨 SonyPerformanceMonitor - Sony风格性能监控器
 *
 * 监控响应式系统的性能指标，提供智能优化建议
 */

struct PerformanceMetrics {
    int frameCount = 0;
    double averageFrameTime = 0.0;
    double maxFrameTime = 0.0;
    int droppedFrames = 0;
    int cacheHitRate = 0;
    int memoryUsageMB = 0;
    bool isSmooth = true;
};

class SonyPerformanceMonitor : public QObject
{
    Q_OBJECT

public:
    explicit SonyPerformanceMonitor(QObject* parent = nullptr);
    ~SonyPerformanceMonitor() = default;

    // === 性能监控接口 ===
    void startMonitoring();
    void stopMonitoring();
    void pauseMonitoring();
    void resumeMonitoring();

    // === 帧率监控 ===
    void beginFrame();
    void endFrame();
    double getCurrentFPS() const;
    double getAverageFPS() const;

    // === 缓存性能 ===
    void recordCacheHit(const QString& cacheType);
    void recordCacheMiss(const QString& cacheType);
    int getCacheHitRate(const QString& cacheType) const;

    // === 内存监控 ===
    void updateMemoryUsage();
    int getCurrentMemoryUsage() const;

    // === 性能报告 ===
    PerformanceMetrics getCurrentMetrics() const;
    void generatePerformanceReport() const;
    QStringList getOptimizationSuggestions() const;

    // === 自动优化 ===
    void enableAutoOptimization(bool enabled);
    bool isAutoOptimizationEnabled() const;

signals:
    void performanceWarning(const QString& warning);
    void optimizationSuggested(const QString& suggestion);
    void frameRateDropped(double currentFPS);
    void memoryUsageHigh(int usageMB);

private slots:
    void onMonitoringTimer();
    void onOptimizationTimer();

private:
    void calculateFrameMetrics();
    void detectPerformanceIssues();
    void applyAutoOptimizations();

    // === 监控状态 ===
    bool m_monitoringActive = false;
    bool m_paused = false;
    bool m_autoOptimizationEnabled = true;

    // === 帧率监控 ===
    QElapsedTimer m_frameTimer;
    QQueue<double> m_frameTimes;
    QElapsedTimer m_monitoringTimer;
    int m_totalFrames = 0;
    int m_droppedFrames = 0;

    // === 缓存监控 ===
    QHash<QString, int> m_cacheHits;
    QHash<QString, int> m_cacheMisses;

    // === 内存监控 ===
    int m_currentMemoryUsage = 0;
    int m_peakMemoryUsage = 0;

    // === 定时器 ===
    QTimer* m_updateTimer;
    QTimer* m_optimizationTimer;

    // === 线程安全 ===
    mutable QMutex m_metricsMutex;

    // === 常量 ===
    static constexpr int FRAME_HISTORY_SIZE = 60;      // 保存60帧的历史
    static constexpr int MONITOR_INTERVAL_MS = 1000;   // 1秒监控间隔
    static constexpr int OPTIMIZATION_INTERVAL_MS = 5000; // 5秒优化间隔
    static constexpr double TARGET_FPS = 60.0;         // 目标帧率
    static constexpr double MIN_SMOOTH_FPS = 30.0;      // 最低流畅帧率
    static constexpr int HIGH_MEMORY_THRESHOLD_MB = 500; // 高内存警告阈值
};

// === 全局实例 ===
class GlobalPerformanceMonitor {
public:
    static SonyPerformanceMonitor& instance();
};

#define PerformanceMonitor() GlobalPerformanceMonitor::instance()

#endif // SONYPERFORMANCEMONITOR_H